<?php
// +--------------------------------------------------------------------------
// | ZAIYOUDAO [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2016 载攸道 版权所有 <TEL:17090888854>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * 用户管理
 */
namespace Research\Controller;
use Common\Controller\AdminController;

/**
 * 用户管理控制器
 */
class AuthController extends AdminController{

	/*
	 * 用户管理
	 */
    public function Info(){
		
		action_log('admin_user_manage');//行为记录
		
		common_public()->assign_page('调研管理');
		
		$m_research_form = M('research_form');
		$m_research_info = M('research_info');
		$m_research_auth = M('research_auth');
		
		$map['form_id'] = I('param.form_id');
		$map['form_uid'] = getUID();
		$map['status'] = 1;
		
		$list = $m_research_auth->where($map)->page($_GET['p'].',12')->select();
		$count      = $m_research_auth->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,12);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		
	
		$this->assign('list',$list);
		$this->assign('form_id',I('param.form_id'));
		
		
		$this->display();
	}
	/*
	 * 用户管理
	 */
    public function Manage(){
		
		action_log('admin_user_manage');//行为记录
		
		common_public()->assign_page('调研管理');
		
		
		$m_research_form = M('research_form');
		$m_research_info = M('research_info');
		
		$map['uid'] = getUID();
		$map['type'] = 1;
		$map['status'] = 1;
		
		$list = $m_research_form->where($map)->order('time desc')->page($_GET['p'].',12')->select();
		$count      = $m_research_form->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,12);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		
		foreach($list as $i=>$k){
			
			$num_map['form_id'] = $list[$i]['id'];
			$num_map['pid'] = 0;
			$num_map['status'] = 1;
			$list[$i]['num'] = $m_research_info->where($num_map)->count();
			
			
		}
	
		$this->assign('list',$list);
		
		
		$this->display();
	}
	
	/*
	 * 新增用户
	 */
    public function Create(){
		
		
			
			
		if(IS_AJAX){
			
			$m_research_auth = M('research_auth');
			
			$map['to_uid'] = I('param.to_uid');
			$map['form_id'] = I('param.form_id');
			
			$result = $m_research_auth->where($map)->find();
			
			if(!$result){
				
				$data['form_uid'] = getUID();
				$data['to_uid'] = I('param.to_uid');
				$data['form_id'] = I('param.form_id');
				$data['status'] = 1;
				$m_research_auth->add($data);
				
			}
			//返回信息
			$info = get_errcode(0);
			
			$this->ajaxReturn($info);
			
		}
		
		
	}
	
	/*
	 * 修改用户
	 */
    public function Edit(){
		
		common_public()->assign_page('修改信息');
		
		$m_research_form = M('research_form');
			
		$map['id'] = I('param.id');
			
		if(IS_POST){
			
			$data['name'] = I('param.name','','');
			$data['value'] = I('param.value','','');
			$data['type'] = 1;
			$data['form_id'] = 0;
			$data['uid'] = getUID();
			$data['ip'] = get_client_ip();
			$data['time'] = time();
			$data['status'] = 1;
			
			$result = $m_research_form->where($map)->save($data);
			
			if($result){
				
				$this->success('编辑成功', U('Research/Form/Manage'));
				
			}else{
				
				$this->error('编辑失败');
				
			}
			
		}else{
			
			
			$data = $m_research_form->where($map)->find();
			
			$this->assign('data',$data);
			
			$this->display();
		}
		
		
	}
	
	/*
	 * 编辑用户信息
	 */
	public function Change($id,$type,$status){
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_research_auth = M('research_auth');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			$types = array(
				
				1 =>'create_form',
				2 =>'edit_form',
				3 =>'find_form',
				4 =>'delete_form',
				5 =>'create_field',
				6 =>'edit_field',
				7 =>'find_field',
				8 =>'delete_field',
				9 =>'create_info',
				10 =>'edit_info',
				11 =>'find_info',
				12 =>'delete_info',
				
				
			);
			
			if($id){
				
			
				
					$map['id'] = $id;
					
					
						
					$data[$types[$type]] = $status;
			
					$m_research_auth->where($map)->save($data);
					
					
					
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	/*
	 * 编辑用户信息
	 */
	public function Status($id,$status=0){
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_research_auth = M('research_auth');
		
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				
				foreach($id as $i=>$k){
					
					$map['id'] = $id[$i];
					
					if($status==0){
						
						
						$m_research_auth->where($map)->delete();
					}else{
						
						$data['create_form'] = 1;
						$data['edit_form'] = 1;
						$data['find_form'] = 1;
						$data['delete_form'] = 1;
						$data['create_field'] = 1;
						$data['edit_field'] = 1;
						$data['find_field'] = 1;
						$data['delete_field'] = 1;
						$data['create_info'] = 1;
						$data['edit_info'] = 1;
						$data['find_info'] = 1;
						$data['delete_info'] = 1;
						
						
						
						$m_research_auth->where($map)->save($data);
					}
						
					
					
					
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	/*
	 * 管理用户群组
	 */
	public function Relevance(){
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_group = M('common_user_group');
			$m_common_user_for_group = M('common_user_for_group');
			
			$type = I('param.type');
			
			$data['uid'] = I('param.uid');
			
			$data['gid'] = I('param.group');
			
			$data['status'] = 1;
			
			
			//是增加还是删除
			if($type==1){
				
				$gmap['id'] = $data['gid'];
				
				$gdata = $m_common_user_group->where($gmap)->find();
				
				//是不是内部组
				if($gdata['type']==1){
					
					//只能同时一个内部组
					$rmap['type'] = 1;
					$rmap['uid'] = $data['uid'];
					
					
					$m_common_user_for_group->where($rmap)->delete();
					
					$data['type'] = 1;
					$result = $m_common_user_for_group->add($data);
					
					
				}else if($gdata['type']==2){
					
					$data['type'] = 2;
					$result = $m_common_user_for_group->add($data);
				}
				
				
				
				
			}else if($type==2){
				
				$result = $m_common_user_for_group->where($data)->delete();
			}
			//获得数据
			
			
			
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	
}